from PySide6.QtWidgets import QApplication, QMessageBox, QFileDialog
from PySide6.QtUiTools import QUiLoader


class DataCompared:
    def __init__(self):
        # 从文件中加载UI定义
        # 从 UI 定义中动态 创建一个相应的窗口对象
        # 注意：里面的控件对象也成为窗口对象的属性了
        # 比如 self.ui.button , self.ui.textEdit
        self.ui = QUiLoader().load('data_compare.ui')
        self.ui.sub_compare.clicked.connect(self.handle_compare)

    def handle_compare(self):
        d_logistic = self.ui.textEdit_logistic.toPlainText()
        d_db = self.ui.textEdit_DataBase.toPlainText()

        dl_logistic = dataToList = list(filter(None, d_logistic.split('\n')))
        dl_db = dataToList = list(filter(None, d_db.split('\n')))
        # 只能过滤空字符和None
        print('OK')
        print(dl_logistic, dl_db)
        # 开始数据比对
        results = set(dl_logistic).intersection(set(dl_db))
        print('dl_logistic:', len(dl_logistic))
        print('results:', len(results))
        if len(dl_logistic) == len(results):

            QMessageBox.about(self.ui,
                              '提示',
                              f'''数据   一致！！'''
                              )
            # 清空文本内容
            self.ui.textEdit_DataBase.clear()
            self.ui.textEdit_logistic.clear()
        else:

            QMessageBox.about(self.ui,
                              '提示',
                              f'''数据    不同！！'''
                              )
            self.ui.textEdit_DataBase.clear()
            self.ui.textEdit_logistic.clear()


if __name__ == '__main__':
    import sys

    app = QApplication(sys.argv)
    win = DataCompared()
    win.ui.show()
    sys.exit(app.exec())
